#!/usr/bin/env python.
# -*- coding: utf-8 -*-

import wx
from core import Core
from core.ide.langsmanager import LangsManager
from core.ide.themesmanager import ThemesManager
from core.ide.commandmanager import CommandManager
from core.ui.centralpanel import CentralPanel
from core.utils.cmdenum import NOTEBOOK_PAGE_CHANGED

class Outline(wx.TreeCtrl):
    def __init__(self):
        self.name="Code Completion"
        wx.TreeCtrl.__init__(self, Core().getMainFrame(), -1,style=wx.TR_DEFAULT_STYLE | wx.TR_FULL_ROW_HIGHLIGHT)
        #self._build()
        self.SetImageList(ThemesManager().getDefaultTheme().getWxLib("icone_16"))
        self.root = self.AddRoot("The Root Item")
        CommandManager().listener(NOTEBOOK_PAGE_CHANGED,self.onPageChange)
        self.Bind(wx.EVT_TREE_ITEM_EXPANDING,self.onExpand)
        
    def build(self):
        print "Outline build"
        isz = (16,16)
        il = wx.ImageList(isz[0], isz[1])
        self.DeleteChildren(self.root)
        #fldridx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, isz))
        #fldropenidx = il.Add(wx.ArtProvider_GetBitmap(wx.ART_FOLDER_OPEN, wx.ART_OTHER, isz))
        self.fileidx     = il.Add(wx.ArtProvider_GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
        self.SetImageList(il)
        
        #self.SetItemImage(self.root, ThemesManager().getDefaultTheme().getWxIdImage("icone_16",'module'), which = wx.TreeItemIcon_Normal)
        #try:
        if Core().getCentralPanel().hasPage() and Core().getCentralPanel().getCurrentPage().getLang() and Core().getCentralPanel().getCurrentPage().getLang().getRootContext:
            self.createContextTree(Core().getCentralPanel().getCurrentPage().getLang().getRootContext(),self.root)
        #except:
        #    pass
    
    def onPageChange(self):
        self.build()
    
    def createContextTree(self,obj,parent):
        if not obj:
            return
        self.DeleteChildren(parent)
        for ind in obj.getChilds():
            child = self.AppendItem(parent, str(ind.name)+'::'+str(ind.getStringTypeValue())+':'+str(ind.type))
            if ThemesManager().getDefaultTheme().getWxIdImage("icone_16",ind.ruleName):
                self.SetItemImage(child, ThemesManager().getDefaultTheme().getWxIdImage("icone_16",ind.ruleName), which = wx.TreeItemIcon_Normal)
            self.SetPyData(child, ind)
            #if True or ind.type=='module' or ind.type=='import':
            self.AppendItem(child,'...')
            #else:
            #    self.createContextTree(ind,child)
            #self.SetItemImage(child, self.fileidx, wx.TreeItemIcon_Normal)
            
            
            
            
    def onExpand(self,e):
        selectedItem = e.GetItem();
        data=self.GetPyData(selectedItem)
        self.createContextTree(data,selectedItem)